package com.tgh.gles2.c3.scissor;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MySurfaceView extends GLSurfaceView {

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    private void init() {
        setEGLContextClientVersion(2);
        setRenderer(new SceneRenderer());
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    
    
    private class SceneRenderer implements Renderer{
        

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClearColor(1, 0, 0, 1);
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            
            GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
            
            GLES20.glClearColor(0, 1, 0, 1);
            GLES20.glScissor(100, 100, 600, 400);
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            
            
            GLES20.glClearColor(0, 0, 1, 1);
            GLES20.glScissor(200, 200, 400, 200);
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            
            GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
        }
        
    }
}
